package com.ebelter.seneo.ui.activitys.sets;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.models.updateApp.UpdateService;
import com.ebelter.seneo.models.updateApp.Updater;
import com.ebelter.seneo.ui.activitys.SeneoBaseActivity;
import com.ebelter.seneo.ui.views.SeneoTipDialog2;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

public class AboutActivity extends SeneoBaseActivity {

    ImageView top_iv;
    TextView top_tv1;
    TextView version_cv_tv;
    TextView version_desc_tv1;
    TextView version_desc_tv2;
    LinearLayout about_ll;

    Updater mUpdater;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_about_ly;
    }

    @Override
    protected void initView() {
        FV();
    }

    private void FV() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        version_cv_tv=findViewById(R.id.version_cv_tv);
        version_desc_tv1=findViewById(R.id.version_desc_tv1);
        version_desc_tv2=findViewById(R.id.version_desc_tv2);
        about_ll=findViewById(R.id.about_ll);

        ViewUtils.hideView(about_ll);

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        about_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (needUpdate&&versionBean!=null){
                    showDialog();
                }
            }
        });
    }

    @Override
    protected void initData() {
        ViewUtils.displayView(top_tv1);
        ViewUtils.setTextViewStr(top_tv1,"Update");

        mUpdater=new Updater();

        String versionInfo = "Current app version:"+ AppUtils.getVerName(this);
        ViewUtils.setTextViewStr(version_cv_tv,versionInfo);

        if (NetUtils.available()){
            mUpdater.checkVersion(this, new Updater.IGetVersion() {
                @Override
                public void callback(boolean needUpdate, UpdateVersionBean versionBean) {
                 deal(needUpdate,versionBean);
                }
            });
        }else {
            deal(false,null);
        }

    }

    private boolean needUpdate;
    private UpdateVersionBean versionBean;

    private void deal(boolean needUpdate1, UpdateVersionBean versionBean1) {
        ViewUtils.displayView(about_ll);
        this.needUpdate=needUpdate1;
        this.versionBean=versionBean1;

        if (needUpdate1){
            ViewUtils.goneView(version_desc_tv2);
            String s1=getString_(R.string.djgx_)+" : "+versionBean1.getFileName();
            ViewUtils.setTextViewStr(version_desc_tv1,s1);
        }else {
            ViewUtils.setTextViewStr(version_desc_tv1,"Current app version");
            ViewUtils.setTextViewStr(version_desc_tv2,getString_(R.string.Latest));
        }
    }

    private void showDialog(){
        SeneoTipDialog2 tipDialog2=new SeneoTipDialog2(this, StringUtils.getResStr(R.string.New_version_is_available_touch_to_update));
        tipDialog2.setListener(new SeneoTipDialog2.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {
                if (!NetUtils.available()){
                    ToastUtil.show(R.string.Network_error);
                    return;
                }
                Intent updateIntent=new Intent(AboutActivity.this,UpdateService.class);
                updateIntent.putExtra(UpdateService.DownloadSer,versionBean);
                AboutActivity.this.startService(updateIntent);
                dialogView.cancel();
            }
        });
        tipDialog2.show();
    }


}
